Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 6, 2026

TUnit0023 incorrectly flags Func<IDisposable> fields as needing disposal, even though the delegate type itself is not disposable.

public class ExampleTest
{
    // Incorrectly triggers: "TUnit0023: _factory should be disposed within a clean up method"
    private readonly Func<IMyInterface> _factory = () => new MyClass();

    [Test]
    public async Task Test1()
    {
        using var t = _factory();  // Caller handles disposal correctly
    }
}

Changes

  • DisposableFieldPropertyAnalyzer.cs: CheckFieldInitializers now verifies the field/property type itself implements IDisposable/IAsyncDisposable, not just that the initializer contains a disposable object creation
  • DisposableFieldPropertyAnalyzerTests.cs: Added test coverage for Func<T> patterns (fields and properties)

Root Cause

The analyzer was checking if any ObjectCreationExpressionSyntax in the initializer creates a disposable type, then flagging the containing field. This incorrectly flags delegate types where the disposable object is created inside a lambda—the delegate itself isn't disposable.

Original prompt

This section details on the original issue you should resolve

<issue_title>[Bug]: Compiler warning TUnit0023 for variable that cannot be disposed</issue_title>
<issue_description>### Description

The warning TUnit0023 is given for a member variable of type Func<IDisposable>, but the member variable itself cannot be disposed of.

Expected Behavior

I would not expect a compiler warning.

Actual Behavior

Compiling the below code results in the warning error TUnit0023: _factory should be disposed within a clean up method.

Steps to Reproduce

Compile the following code in a TUnit project:

public interface IMyInterface : IDisposable
{
}

public class MyClass : IMyInterface
{
    public void Dispose()
    {
        Console.WriteLine("disposed");
    }
}

public class ExampleTest
{
    private readonly Func<IMyInterface> _factory = () => new MyClass();

    [Test]
    public async Task Test1()
    {
        using var t = _factory();
    }
}

TUnit Version

1.9.2

.NET Version

.NET 10

Operating System

Windows

IDE / Test Runner

dotnet CLI (dotnet test / dotnet run)

Error Output / Stack Trace

error TUnit0023: _factory should be disposed within a clean up method

Additional Context

Image

IDE-Specific Issue?

  • I've confirmed this issue occurs when running via dotnet test or dotnet run, not just in my IDE</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI changed the title [WIP] Fix compiler warning TUnit0023 for IDisposable variable Fix TUnit0023 false positive for Func<IDisposable> fields Jan 6, 2026
Copilot AI requested a review from thomhurst January 6, 2026 19:59
@thomhurst thomhurst marked this pull request as ready for review January 6, 2026 20:41
Copilot AI review requested due to automatic review settings January 6, 2026 20:41
@thomhurst thomhurst enabled auto-merge (squash) January 6, 2026 20:41
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a false positive in the TUnit0023 analyzer that incorrectly flagged Func<IDisposable> fields and properties as needing disposal. The issue occurred because the analyzer was checking if any object creation within an initializer created a disposable type, without verifying that the field/property type itself was disposable.

Key Changes:

  • Added a secondary check to verify the field/property type itself implements IDisposable or IAsyncDisposable before flagging it
  • Added comprehensive test coverage for Func<IDisposable> patterns in fields and properties

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
TUnit.Analyzers/DisposableFieldPropertyAnalyzer.cs Added checks to verify field and property types themselves are disposable before flagging them, fixing false positives for delegate types like Func<IDisposable>
TUnit.Analyzers.Tests/DisposableFieldPropertyAnalyzerTests.cs Added three new tests covering Func<IDisposable> fields and properties with both interface and concrete disposable types

This was referenced Jan 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Compiler warning TUnit0023 for variable that cannot be disposed

2 participants